<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 区域管理ajax类 
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysRegion.class.php');

class ajax_areaManage {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1://显示市区
                    $this->addMidArea();
                    break;

                case 2://隐藏市区
                    $this->removeArea();
                    break;

                case 3://删除区域
                    $this->deleteArea();
                    break;

                case 4://获取省
                    $this->getProvince();
                    break;

                case 5://获取省
                    $this->getCity();
                    break;

                case 6://保存 编辑
                    $this->saveArea();
                    break;

                case 7: //生成地址缓存
                    $this->createCache();
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {
            $this->toReturn('参数错误');
        }


    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    private function createCache() {

        global $G_PGLOB;

        //add by allen.qiang 20170118 缓存数据更新 start
        $G_PGLOB->setCache('city');
        //add by allen.qiang 20170118 缓存数据更新 end

        $sysRegion = new sysRegion( $G_PGLOB->DBCA );

        $arg_array = array(
            'parentID' => 0,
            'sortname' => 'sortOrder',
            'sortorder' => 'ASC'
        );

        //地址数据
        $region_array = array();

        //查询省份数据
        $province_array = $sysRegion->getAreaManageList( $arg_array );
        if (!$province_array) {
            $this->toReturn('区域数据没有找到', 404);
        }

        foreach($province_array as &$province) {

            $temp_province_array = array(
                'n' => $province['name'],
                'c' => array()
            );

            //查询市区数据
            $arg_array = array(
                'parentID' => $province['id'],
                'sortname' => 'sortOrder',
                'sortorder' => 'ASC'
            );
            $city_array = $sysRegion->getAreaManageList( $arg_array );
            if (!$city_array) {
                continue;
            }

            foreach($city_array as &$city) {

                $cityID = &$city['id'];

                $temp_province_array['c'][$cityID] = array(
                    'n' => $city['name'],
                    'a' => null,
                    'c' => array()
                );
                $temp_city_array = &$temp_province_array['c'][$cityID];

                //查询县/区
                $arg_array = array(
                    'parentID' => $cityID,
                    'sortname' => 'sortOrder',
                    'sortorder' => 'ASC'
                );
                $county_array = $sysRegion->getAreaManageList( $arg_array );
                if (!$county_array) {
                    continue;
                }

                foreach($county_array as &$county) {
                    $temp_city_array['c'][$county['id']] = array(
                        'n' => $county['name'],
                        'p' => null
                    );
                }

            }

            $region_array[$province['id']] = $temp_province_array;

        }

        if (empty($region_array)) {
            $this->toReturn('区域数据没有找到', 404);
        }

        //加载文件类
        quanli_include('module/base/common/quanliFile.class.php');

        //实例文件类
        $quanliFile = new quanliFile( $G_PGLOB );

        //修改区域内容
        $region_str = 'E.region={list: ' . json_encode($region_array) . '};';
        $region_address =  G_APPLICATION_BASEPATH . '/inc/config/default/region.min.js';
        $quanliFile->write( $region_address,$region_str );

        $this->toReturn('刷新成功', 200);


    }

    //显示市区
    private function addMidArea(){

        global $G_PGLOB;

        $edit_permission = $G_PGLOB->has_permission(12);;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['sortname'] = 'sortOrder';

        $arg_array['sortorder'] = 'ASC';

        //判断点击大分类还是中分类
        if(isset($_REQUEST['mid']) && $_REQUEST['mid'] == 'mid'){

            $arg_array['parentID'] = $_REQUEST['cate_id'];//中分类ID

            $result = $sysRegion->getAreaManageList($arg_array);

            if($result){

                $str = '';
                //组织HTML
                foreach($result as $val){

                    $cat_str = $val['id'];

                    $str  .= '<tr id="scate'.$val['id'].'">';
                    $str  .= '<td style="text-align:left;">
                    <div style = "float:left;margin-left:50px;width:300px">
                    <div id="tdone_s_'.$val['id'].'" style="float:left">
                      <div class="left childImg" ></div>
                    </div>

                        <div id = "s_cell1_'.$val['id'].'" style="text-align:left;">'.$val['name'].'
                        </div>
                    </div>
                    </td>';
                    $str  .= '<td  class = "tal"><div id = "s_cell2_'.$val['id'].'">'.'县/区'.'</div></td>';
                    $str  .= '<td  class = "tac">' . $val['sortOrder'] . '</td>';

                    if($edit_permission){
                        $str  .= '<td style="text-align:center">
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="editCategory" onclick = "category.add(\'s\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="delCategory" onclick = "category.catedelete(\'s\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                    }
                    $str  .= '</tr>';
                }

                $str_arr = array('tr'=>$str);

            }


        }else{

            $arg_array['parentID'] = $_REQUEST['cate_id'];

            $result = $sysRegion->getAreaManageList($arg_array);

            if($result){

                $str = '';
                //组织HTML
                foreach($result as $val){

                    $cat_str = $val['id'];

                    if($val['hasMid']){
                        $img = '<div id="img_add_1" onclick="category.add_cate('.$val['id'].',\''.'mid'.'\')" class="left closeImg"></div>';
                    }else{
                        $img =  '';
                    }

                    $str  .= '<tr id="mcate'.$val['id'].'">';
                    $str  .= '<td style="text-align:left;">
                    <div style = "float:left;margin-left:25px;width:200px">
                        <div style = "float:left" id="tdone_m_'.$val['id'].'">'.$img.'</div>
                        <div id = "m_cell1_'.$val['id'].'" style="text-align:left;">'.$val['name'].'
                        </div>
                    </div>
                    </td>';
                    $str  .= '<td class = "tal">'.'<div id = "m_cell2_'.$val['id'].'">市</div>'.'</td>';
                    $str  .= '<td  class = "tac">' . $val['sortOrder'] . '</td>';
                    if($edit_permission){
                        $str  .= '<td style="text-align:center">
                        <div class="childCategory" onclick="category.addChild(\'m\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="editCategory" onclick = "category.add(\'m\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="delCategory" onclick = "category.catedelete(\'m\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                    }
                    $str  .= '</tr>';
                }

                $str_arr = array('tr'=>$str);
            }

        }

        echo json_encode($str_arr);
    }

    //移除对应分类
    private function removeArea(){

        global $G_PGLOB;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        //判断是点击大分类移除还是小分类移除
        if($_REQUEST['mid'] == ''){

            $arg_array['parentID'] = $_REQUEST['cate_id'];

            //ID数组
            $idArr = array();

            $result = $sysRegion->searchMidRegion($arg_array);

            if($result){

                foreach($result as $val){

                    $idArr[] = $val['id'];

                    $searchSmallArr = array(
                        'parentID'=>$val['id']
                    );

                    $smallResult = $sysRegion->searchMidRegion($searchSmallArr);

                    if($smallResult){

                        foreach($smallResult as $smallVal){
                            $idArr[] =  $smallVal['id'];
                        }

                    }

                }
            }

        }else{

            $arg_array['parentID'] = $_REQUEST['cate_id'];

            //中分类ID数组
            $idArr = array();

            $result = $sysRegion->searchMidRegion($arg_array);

            if($result){

                foreach($result as $val){

                    $idArr[] =  $val['id'];
                }

            }

        }
        //misArr :中分类ID数组 smlArr:小分类ID数组
        echo json_encode(array('idArr'=>$idArr));

    }

    //删除区域
    private function deleteArea(){

        global $G_PGLOB;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $result = $sysRegion->deleteAreaManage($arg_array);

        echo json_encode($result);

    }

    //获取省
    private function getProvince(){

        global $G_PGLOB;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array['parentID'] = 0;

        $result_data = $sysRegion->getAreaManageList($arg_array);



        //编辑：获取对应分类信息
        if(isset($_REQUEST['editStr']) && !empty($_REQUEST['editStr'])){

            $editStr = $_REQUEST['editStr'];

            $mark = $_REQUEST['mark'];

            switch($mark){
                case 'b':
                    $bigid_arr = array(
                        "id" => $editStr
                    );
                    $result = $sysRegion->getAreaManageList($bigid_arr);
                    break;
                case 'm':
                    $midid_arr = array(
                        "id" => $editStr
                    );
                    $result = $sysRegion->getAreaManageList($midid_arr);
                    //获取中分类信息
                    $midid_arr = array(
                        "parentID" => $result[0]['parentID']
                         );

                     $result['midCate'] = $sysRegion->getAreaManageList($midid_arr);
                    break;
                case 's':
                    $smaid_arr = array(
                        "id" => $editStr
                    );
                    $result = $sysRegion->getAreaManageList($smaid_arr);

                    //获取中分类信息
                    $midid_arr = array(
                        "id" => $result[0]['parentID']
                         );

                    $mid_result = $sysRegion->getAreaManageList($midid_arr);

                    $big_arr = array(
                        'parentID' => $mid_result[0]['parentID']
                        );

                    $result['midCate'] = $sysRegion->getAreaManageList($big_arr);

                    break;
            }

            if($result){
                $result_data['editData'] = $result;
            }

        }

        if(!$result_data){
            $result_data = array('emsg'=>1);
        }

        echo json_encode($result_data);

    }

    //获取市
    private function getCity(){

        global $G_PGLOB;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array['parentID'] = $_REQUEST['id'];

        $result_data = $sysRegion->getAreaManageList($arg_array);

        echo json_encode($result_data);

    }

    //保存 编辑
    private function saveArea(){

        global $G_PGLOB;

        $sysRegion = new sysRegion($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['creator'] = $G_PGLOB->currentUserID;

        switch($_REQUEST['editcate']){
            case 'b'://保存 编辑大分类
                $result = $sysRegion->saveProvince($arg_array);
                break;
            case 'm'://保存 编辑中分类
                $result = $sysRegion->saveCity($arg_array);
                break;
            case 's'://保存 编辑小分类
                $result = $sysRegion->saveArea($arg_array);
                break;
        }

        echo json_encode($result);

    }


}
